﻿using Kinlo.Entitys;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection.PortableExecutable;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using XCode.Membership;
using XingLucifer.IBase.Enums;

namespace Kinlo.MESDocking
{
    public abstract class MESInteractionBase : IMESInteraction
    {
        protected Action<string, string, string, string, string,
            long, DateTime, string, string, string, string> _log;
        protected Action<string, string, string> _dialog;
        protected Stopwatch _stopwatch;
        protected string _name;
        public MESInteractionBase(Action<string, string, string, string, string, long, DateTime, string, string, string, string> log, Action<string, string, string> dialog, string name)
        {
            _log = log;
            _dialog = dialog;
            _name = name;
            _stopwatch = new Stopwatch();
        }
        public virtual ResultTypes Interaction(string url, int timeout, int reconnect_count, DateTime dateTime, ProductionData[] production, Dictionary<string, object> parameters)
        {
            return ResultTypes.合格;

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="url">路径</param>
        /// <param name="timeout">超时时间</param>
        /// <param name="reconnect_count">失败重试次数</param>
        /// <param name="dateTime">当前时间</param>
        /// <param name="parameters">参数</param>
        /// <returns></returns>
        public virtual ResultTypes MESInteraction(string url, int timeout, int reconnect_count, DateTime dateTime, Dictionary<string, object> parameters)
        {
            return ResultTypes.合格;

        }



        public virtual async Task<ResultTypes> MESInteractionAsync(string url, int timeout, int reconnect_count, DateTime dateTime, Dictionary<string, object> parameters)
        {
            // 异步操作
            var result = await SomeAsyncOperation();

            // 其他逻辑...

            // 返回结果
            return result;

        }
        private async Task<ResultTypes> SomeAsyncOperation()
        {
            // 在这里执行异步操作，例如调用异步 API、数据库查询等
            // 使用 await 等待异步操作完成
            // ...

            // 返回异步操作的结果
            return ResultTypes.合格;
        }
        public (bool, string) Post(string parameter, string url, int outtime, int reconnect_count)
        {
            while (reconnect_count > 0)
            {
                try
                {
                    HttpWebRequest request = WebRequest.CreateHttp(url);
                    request.ContentType = "application/json";
                    request.Method = "POST";
                    request.ProtocolVersion = HttpVersion.Version11;
                    request.Timeout = outtime;
                    //Match host = Regex.Match(url, _urlRegex);
                    //request.Host = host.Value;
                    byte[] bytes = Encoding.UTF8.GetBytes(parameter);
                    request.ContentLength = bytes.Length;
                    using (Stream stream = request.GetRequestStream())
                    {
                        stream.Write(bytes, 0, bytes.Length);
                    }
                    string resultJson = string.Empty;
                    using (var response = (HttpWebResponse)request.GetResponse())
                    {
                        using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                        {
                            resultJson = reader.ReadToEnd();
                        }
                    }
                    return new(true, resultJson);
                    //var response = request.GetResponse();
                    //byte[] byte1 = new byte[2048];
                    //int length = 0;
                    //using (Stream stream = response.GetResponseStream())
                    //{
                    //    length = stream.Read(byte1, 0, 2048);
                    //}
                    //return new(true, Encoding.UTF8.GetString(byte1.Take(length).ToArray()));
                }
                catch (Exception ex)
                {
                    reconnect_count--;
                    if (reconnect_count == 0)
                    {
                        return new(false, ex.ToString());
                    }
                }
            }
            return new(false, "执行错误");
        }
    }
}
